package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.time.Duration;

public class LoginPage extends Utils {
    public static String url = "http://8.137.19.140:9090/blog_login.html";

    public LoginPage() {
        super(url);
    }

    public void loginPageRight(){
        //通过检查页面元素来查看页面是否加载成功
        //检查两个输入框有没有
        driver.findElement(By.cssSelector("#username"));
        driver.findElement(By.cssSelector("#password"));
    }

    //检查登录功能，成功登录
    public void LoginSuc() throws IOException, InterruptedException {
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();

        driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#submit")).click();
        //检查点击登陆之后是否登陆成功
        driver.findElement(By.cssSelector("body > div.container > div.left > div"));
        //页面标题来检查是否登陆成功
//        Thread.sleep(3000);
//        getScreenShot(getClass().getName());
        String expect = driver.getTitle();
        assert expect.equals("博客列表页");

        getScreenShot(getClass().getName());

        driver.navigate().back();
    }
    //检查登录功能，失败登录
    public void LoginFail() throws IOException, InterruptedException {
        driver.navigate().refresh();
        driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
        driver.findElement(By.cssSelector("#password")).sendKeys("12345");
        driver.findElement(By.cssSelector("#submit")).click();

        //处理弹窗，打印登录失败原因
        //用显示等待等弹窗出现
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(3));
        wait.until(ExpectedConditions.alertIsPresent());
//        Thread.sleep(2000);
        Alert alert = driver.switchTo().alert();
        String alertText = alert.getText();
        System.out.println(alertText);
        alert.accept();


        //查看是否登录成功-预期结果是不成功
        String text = driver.getTitle();
        assert text.equals("博客登陆页");
//        getScreenShot(getClass().getName());
    }
}
